package servlets;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import beans.Enseignant;
import beans.Etudiant;
import beans.Secretaire;
import beans.Utilisateur;
import forms.ConnectionForm;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Miren
 */
@WebServlet(name = "Connection", urlPatterns = {"/Connection"})
public class Connection extends HttpServlet {
    public static final String ATT_USER = "utilisateur";
    public static final String ATT_FORM = "form";
    public static final String ATT_ERREUR = "erreur";
    public static final String VUE = "/connect.jsp";
    public static final String VUE_ENSEIGNANT = "/enseignant.jsp";
    
    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    public void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException{
        /* Affichage de la page d'inscription */
        request.setAttribute(ATT_ERREUR, false);
        this.getServletContext().getRequestDispatcher( VUE ).forward( request, response );
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
                
            /* Préparation de l'objet formulaire */
            ConnectionForm form = new ConnectionForm();
            
             /* Appel au traitement et à la validation de la requête, et récupération du bean en résultant */
        try {    
            Utilisateur utilisateur = form.checkUtilisateur( request );
            
            if(utilisateur==null){
                request.setAttribute(ATT_ERREUR, true);
                this.getServletContext().getRequestDispatcher( VUE ).forward( request, response );
            }
            else{        
                /* Stockage du formulaire et du bean dans l'objet request */
                //request.setAttribute( ATT_FORM, form );
                request.setAttribute( ATT_USER, utilisateur );
                if(utilisateur instanceof Enseignant){
                    this.getServletContext().getRequestDispatcher( VUE_ENSEIGNANT ).forward( request, response );
                }
                if(utilisateur instanceof Secretaire){
                    //todo
                }
                if(utilisateur instanceof Etudiant){
                    //todo
                }
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
